﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Text.RegularExpressions;

namespace SinaApi
{
    class Serializer
    {

    }

    public static class XmlSerializeHelper
    {
        public static T Deserialize<T>(string xmlDatas, Encoding encoding) where T : class
        {
            byte[] buffer = encoding.GetBytes(xmlDatas);
            using (MemoryStream ms = new MemoryStream(buffer))
            {
                T t = default(T);
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                try
                {
                    t = serializer.Deserialize(ms) as T;
                    return t;
                }
                catch
                {
                    return default(T);
                }
            }
        }

        public static string Serialize<T>(T obj) where T : class
        {

            using (MemoryStream ms = new MemoryStream())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                serializer.Serialize(ms, obj);
                ms.Position = 0;
                using (StreamReader sr = new StreamReader(ms))
                {
                    return sr.ReadToEnd();
                }

            }
        }
    }

    //public static class HtmlSerializeHelper
    //{
    //    public static StatusCollection Deserialize(string htmlDatas, DateTime sinceDate)
    //    {

    //        StatusCollection statuses = new StatusCollection();
    //        statuses.items = new List<Status>();
    //        Regex r = new Regex(@"<p class=""sms"" mid=""(\d+)"" type=""1""><a href=""http://t.sina.com.cn/(\d+)"" [^>]*>([^<>]*)</a>：(.*)</p>");
    //        Regex r2 = new Regex(@"<strong date=""([^""]*)"">");
    //        MatchCollection ms = r.Matches(htmlDatas);
    //        MatchCollection datems = r2.Matches(htmlDatas);
    //        for (int i = 0; i < ms.Count; i++)
    //        {
    //            Match m = ms[i];
    //            Status status = new Status();
    //            status.id = Int64.Parse(m.Groups[1].Value);
    //            status.text = RemoveHtml(m.Groups[4].Value);
    //            status.created_date = DateTime.Parse(datems[i].Groups[1].Value);
    //            status.user = new User()
    //            {
    //                id = Int64.Parse(m.Groups[2].Value),
    //                name = m.Groups[3].Value,
    //                screen_name = m.Groups[3].Value,
    //            };
    //            if (status.created_date.HasValue && status.created_date > sinceDate)
    //            {
    //                statuses.items.Add(status);
    //            }
    //            //Console.WriteLine(m.Value);
    //        }
    //        return statuses;
    //    }

    //    /// <summary>
    //    /// 移除Html标记
    //    /// </summary>
    //    /// <param name="content"></param>
    //    /// <returns></returns>
    //    public static string RemoveHtml(string content)
    //    {
    //        string regexstr = @"<[^>]*>";
    //        return Regex.Replace(content, regexstr, string.Empty, RegexOptions.IgnoreCase);
    //    }
    //}
}
